//
//  YZMLockIndicatorView.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/4/20.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMLockIndicatorView.h"

static const float lock_indicator_circle_margin = 8.0;
static const float lock_indicator_circle_diameter = 10.0;
static const float lock_indicator_tag_for_base = 10000;

@implementation YZMLockIndicatorView{
    
    NSMutableArray *_buttonArray;
}

- (id)init{
    
    self = [super init];
    if (self) {
        
        self.clipsToBounds = YES;
        [self initCircles];
        [self mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.width.height.mas_equalTo(3*lock_indicator_circle_diameter +2*lock_indicator_circle_margin);
        }];
    }
    
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.clipsToBounds = YES;
        [self initCircles];
    }
    return self;
}

- (void)initCircles
{
    _buttonArray = [NSMutableArray array];
    
    // 初始化圆点
    for (int i=0; i<9; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        int x = (i%3) * (lock_indicator_circle_diameter+lock_indicator_circle_margin);
        int y = (i/3) * (lock_indicator_circle_diameter+lock_indicator_circle_margin);
        [button setFrame:CGRectMake(x, y, lock_indicator_circle_diameter, lock_indicator_circle_diameter)];
        //button.backgroundColor = [YZMAppStyle colorForGrayText];
        button.backgroundColor = [UIColor clearColor];
        [button setBackgroundImage:[self.class createImageWithColor:[YZMAppStyle colorForIconColor] withFrame:CGRectMake(0, 0, lock_indicator_circle_diameter, lock_indicator_circle_diameter)] forState:UIControlStateSelected];
        button.userInteractionEnabled= NO;//禁止用户交互
        button.tag = i + lock_indicator_tag_for_base + 1; // tag从基数+1开始,
        [button.layer setCornerRadius:button.frame.size.width/2];
        [button.layer setMasksToBounds:YES];
        button.layer.borderColor = [YZMAppStyle colorForLightGrayText].CGColor;
        button.layer.borderWidth = 1;
        [self addSubview:button];
        [_buttonArray addObject:button];
    }
    
    self.backgroundColor = [UIColor clearColor];
}

- (void)setPasswordString:(NSString*)string
{
    for (UIButton* button in _buttonArray) {
        [button setSelected:NO];
        button.layer.borderWidth = 1;
    }
    
    NSMutableArray* numbers = [[NSMutableArray alloc] initWithCapacity:string.length];
    for (int i=0; i<string.length; i++) {
        NSRange range = NSMakeRange(i, 1);
        NSNumber* number = [NSNumber numberWithInt:[string substringWithRange:range].intValue-1]; // 数字是1开始的
        [numbers addObject:number];
        [_buttonArray[number.intValue] setSelected:YES];
        UIButton *button = _buttonArray[number.intValue];
        button.layer.borderWidth = 0;
    }
}

+ (UIImage *)createImageWithColor:(UIColor *)color withFrame:(CGRect)frame
{
    UIGraphicsBeginImageContext(frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, frame);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

@end
